#include "csysconfig.h"
#include <QMessageBox>
#include "cglobal.h"
#include "cpublicdata.h"

CSysConfig::CSysConfig(QWidget *parent)
    : QDialog(parent)
{
    ui.setupUi(this);
    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
    initConnect();
    initUi();
}

CSysConfig::~CSysConfig()
{}

void CSysConfig::initConnect()
{
    connect(ui.pushButtonOK, &QPushButton::clicked, this, &CSysConfig::slotPushButtonOK);
    connect(ui.pushButtonCancle, &QPushButton::clicked, this, &CSysConfig::slotPushButtonCancel);
    connect(ui.checkBoxAutoStart, &QCheckBox::toggled, this, [=](bool bChecked){
        ui.checkBoxAutoStart->setText(bChecked ? A2T("是") : A2T("否"));
    });
}

void CSysConfig::initUi()
{
    this->setWindowTitle(A2T("系统配置"));
    ui.comboBoxDriver->addItem("SyncbaseRW");
    SServerCfg stServerCfg = CPublicData::instance().m_stServerCfg;
    ui.lineEditServerIp->setText(stServerCfg.m_sServerIp);
    ui.lineEditServerPort->setText(QString::number(stServerCfg.m_nPort));
    ui.lineEditClientMax->setText(QString::number(stServerCfg.m_nClientMax));
    ui.checkBoxAutoStart->setChecked(CPublicData::instance().m_bAutoStart);
    ui.spinBoxTimeOut->setValue(stServerCfg.m_nTagTimeOut);
}

void CSysConfig::slotPushButtonOK()
{
    int ret = QMessageBox::information(this, A2T("提示"), A2T("是否确认保存?"), QMessageBox::Yes, QMessageBox::No);
    if (ret == QMessageBox::No)
    {
        return;
    }
    SServerCfg stServerCfg;
    stServerCfg.m_sServerIp = ui.lineEditServerIp->text();
    stServerCfg.m_nPort = ui.lineEditServerPort->text().toInt();
    stServerCfg.m_nClientMax = ui.lineEditClientMax->text().toInt();
    stServerCfg.m_nTagTimeOut = ui.spinBoxTimeOut->value();
    CPublicData::instance().m_bAutoStart = ui.checkBoxAutoStart->isChecked();
    CPublicData::instance().m_stServerCfg = stServerCfg;
    CPublicData::instance().saveIni();
    this->accept();
}

void CSysConfig::slotPushButtonCancel()
{
    this->close();
}
